// @Author EthanScriptOn
// @Desc
package processor

import (
	"context"
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/util"
	"reflect"
)

// ProcessorType The processor type is used to determine whether it is a processor interface
var ProcessorType = reflect.TypeOf((*Processor)(nil)).Elem()

// Processor Processor interface
type Processor interface {
	// Process processing, data is the data to be processed
	Process(ctx context.Context, data interface{}) error
	// Initialization Initialize the interface
	Initialization() (err error)
}

// MakeProcessor Create a processor based on the type
func MakeProcessor(processorType reflect.Type) (Processor, error) {
	if !processorType.Implements(ProcessorType) {
		return nil, errors.New("the type must implement Processor interface")
	}
	newInstance := util.NewInstance(processorType).Interface()
	processor, ok := newInstance.(Processor)
	if !ok {
		return nil, errors.New("conversion exception the type must implement Processor interface")
	}
	return processor, nil
}
